package ConfigALL

import (
	"Intelligence/Util/Journal"
	"Intelligence/Util/Public"
	"errors"
	"fmt"
	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/encoding/gyaml"
	"io/ioutil"
)

var (
	_config *gjson.Json
)

//读配置文件信息
func RunConfig()error  {
	riJiAlL,_:= Journal.GetRiJi()
	riJi:=riJiAlL[0]

	//是否是Linux环境
	filename:="configW.yml"
	{
		_LinuxOn, err := Public.GetOnLinux环境()
		if err != nil {
			return err
		}
		if _LinuxOn {
			filename="config.yml"
		}
	}

	configByte, err := ioutil.ReadFile(filename)
	if err != nil {
		errValue:=fmt.Sprint("读取配置信息错误 - ",err.Error())
		riJi.RiJiShuChuJingGaoFatal(errValue)
		return errors.New(errValue)
	}
	configYml, err :=gyaml.ToJson(configByte)
	if err != nil {
		errValue:=fmt.Sprint("解析配置信息错误 - ",err.Error())
		riJi.RiJiShuChuJingGaoFatal(errValue)
		return errors.New(errValue)
	}

	_config, err = gjson.DecodeToJson(configYml)
	if err != nil {
		errValue:=fmt.Sprint("解析配置信息错误 - ",err.Error())
		riJi.RiJiShuChuJingGaoFatal(errValue)
		return errors.New(errValue)
	}

	return nil
}

//取指定配置信息
func GetConfig(name string) interface{} {
	if _config == nil {
		return ""
	}
	data:= _config.Get(name)
	if data==nil {
		return ""
	}
	return data
}
